package com.sdkx.app.fragment;


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.WayPointManager;
import com.sdkx.app.activitys.NearestActivity;
import com.sdkx.app.adapter.SearchAdapter;
import com.sdkx.app.model.WayPoint;
import com.sdkx.app.model.ViewObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NearestDistanceFragment extends Fragment {
    private final String TAG = NearestDistanceFragment.class.getSimpleName();
    SearchAdapter searchAdapter;
    List<ViewObject> listDataSource = new ArrayList<>();
    private Double currentLongitude;
    private Double currentLatitude;
    private LinearLayout progressbarContainer;
    private RadioGroup radioGroup;

    public NearestDistanceFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        NearestActivity nearestActivity = (NearestActivity) getActivity();
        currentLatitude = nearestActivity.currentLatitude;
        currentLongitude = nearestActivity.currentLongitude;
        // Inflate the marktypeitem for this fragment
        View view = inflater.inflate(R.layout.fragment_nearest_distance, container, false);
        radioGroup = (RadioGroup)view.findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                onRadioGroupCheckedChanged(i);
            }
        });

        ListView listView = (ListView)view.findViewById(R.id.listView);
        searchAdapter = new SearchAdapter(getActivity(),R.layout.search_listview_item,listDataSource);
        listView.setAdapter(searchAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                searchAdapter.setSelectPosition(position);
                searchAdapter.notifyDataSetInvalidated();
            }
        });

        progressbarContainer = (LinearLayout)view.findViewById(R.id.progressbar_layout);

        new LoadTask().execute("all");
        return view;
    }

    private void onRadioGroupCheckedChanged(int radioBtnId){
        LoadTask loadTask = new LoadTask();
        switch (radioBtnId) {
            case R.id.radioButton_all:
                loadTask.execute("all");
                break;
            case R.id.radioButton_airport:
                loadTask.execute("airport");
                break;
            case R.id.radioButton_airpoint:
                loadTask.execute("airpoint");
                break;
            default:
                break;
        }
    }
    //获取最近使用的数据源
    private List<ViewObject> getNearestAll() {
        List<ViewObject> tempList = new ArrayList<>();
        if(currentLongitude==180 || currentLatitude == 180){
            return tempList;
        }
        //添加点类型的数据
        List<WayPoint> wayPoints = WayPointManager.getItemsByDistance(
                currentLongitude,
                currentLatitude,
                10,
                null
        );
        tempList.addAll(wayPoints);
        //TODO: 添加其他类型的数据源数据
        return tempList;
    }

    private List<WayPoint> getNearestAirport(){
        List<WayPoint> tempList = new ArrayList<>();
        if(currentLongitude==180 || currentLatitude == 180){
            return tempList;
        }
        return WayPointManager.getItemsByDistance(
                currentLongitude,
                currentLatitude,
                10,
                new int[]{WayPoint.WayPointType.AIS_AIRPORT.ordinal(),
                        WayPoint.WayPointType.AM_AIRPORT.ordinal(),
                        WayPoint.WayPointType.MILITARY_AIRPORT.ordinal(),
                        WayPoint.WayPointType.PERSON_AIRPORT.ordinal()
                });
    }

    private List<WayPoint> getNearestAirpoint(){
        List<WayPoint> tempList = new ArrayList<>();
        if(currentLongitude==180 || currentLatitude == 180){
            return tempList;
        }
        return WayPointManager.getItemsByDistance(
                currentLongitude,
                currentLatitude,
                10,
                new int[]{WayPoint.WayPointType.PERSON_POINT.ordinal(),
                        WayPoint.WayPointType.MILITARY_POINT.ordinal(),
                        WayPoint.WayPointType.AIS_POINT.ordinal()
                });
    }

    private class LoadTask extends AsyncTask<String,Void,Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressbarContainer.setVisibility(View.VISIBLE);
            for(int i=0;i<radioGroup.getChildCount();i++){
                radioGroup.getChildAt(i).setEnabled(false);
            }
        }

        @Override
        protected Void doInBackground(String... strings) {
            listDataSource.clear();
            switch (strings[0]) {
                case "all":
                    listDataSource.addAll(getNearestAll());
                    break;
                case "airport":
                    listDataSource.addAll(getNearestAirport());
                    break;
                case "airpoint":
                    listDataSource.addAll(getNearestAirpoint());
                    break;
                default:
                    break;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressbarContainer.setVisibility(View.GONE);
            for(int i=0;i<radioGroup.getChildCount();i++){
                radioGroup.getChildAt(i).setEnabled(true);
            }
            searchAdapter.setSelectPosition(-1);
            searchAdapter.notifyDataSetInvalidated();
        }
    }
}
